package com.glory.wxelectricitypda.activity;

import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.BatchReturnInventoryAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.EqpEntoty;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

public class BatchInventoryActivity extends BaseDialogActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;

    @BindView(R.id.tvTowareHouseID)
    FreeUI_ClearEditText tvTowareHouseID;

    @BindView(R.id.tvTostorageID)
    FreeUI_ClearEditText tvTostorageID;

    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;


    @BindView(R.id.rvRelocationInfoList)
    RecyclerView rvRelocationInfoList;

    @BindView(R.id.etMainQty)
    FreeUI_ClearEditText etMainQty;

    private BatchReturnInventoryAdapter mRelocationAdapter;



    @Override
    protected int getLayoutResId() {
        return R.layout.activity_batch_inventory;
    }

    @Override
    protected void initView() {

        freeUI_titleView.setRightListener("提交", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mRelocationAdapter.getData().size() == 0) {
                    showToast("请添加转移库信息", false);
                    return;
                }

                submit();
            }
        });

        rvRelocationInfoList.setLayoutManager(new LinearLayoutManager(BatchInventoryActivity.this));
        rvRelocationInfoList.setItemAnimator(new DefaultItemAnimator());
        rvRelocationInfoList.addItemDecoration(new DividerItemDecoration(BatchInventoryActivity.this, 1));
        mRelocationAdapter = new BatchReturnInventoryAdapter(null);
        mRelocationAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mRelocationAdapter.isFirstOnly(true);
        rvRelocationInfoList.setAdapter(mRelocationAdapter);

    }

    @Override
    protected void initData() {

        initEdit(etLotId, s -> {
            if (s.replace("|",",").split(",").length > 2){
                etLotId.setText(s.replace("|",",").split(",")[2]);
            }
            if (etMainQty.getText().toString().equals("")) {
                showToast("请输入数量", false);
                return;
            }
            EqpEntoty.MaterierlBean materierlBean = new EqpEntoty.MaterierlBean();
            materierlBean.setMAINQTY(etMainQty.getText().toString());
            materierlBean.setMLOTID(etLotId.getText().toString());
            mRelocationAdapter.addData(0,materierlBean);
        });


    }


    private void submit() {
        JSONObject map = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            map.put("TOWAREHOUSEID", tvTowareHouseID.getText().toString().trim());
            map.put("TOSTORAGEID", tvTostorageID.getText().toString().trim());
            map.put("TOSTORAGETYPE", "receive");
            map.put("ACTIONCODE", "A");
            map.put("RETURNFLAG", "PANDIAN");
            JSONArray jsonArray = new JSONArray(new Gson().toJson(mRelocationAdapter.getData()));
            map.put("MLOTLIST", jsonArray);
            jsonObject.put("Data", map);
            jsonObject.put("MessageName", "EUIT.EuitTransferMLot");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<EqpEntoty.MKittingBean>>(
                new TypeToken<ResponseBean<EqpEntoty.MKittingBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<EqpEntoty.MKittingBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showToast("转移成功", true);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    }, 2000);
                }
            }

            @Override
            public void onError(Response<ResponseBean<EqpEntoty.MKittingBean>> response) {
                super.onError(response);
                loadError(response.getException(), "EUIT.EuitTransferMLot");
            }
        });
    }



    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("批次盘点");
        return new FreeUI_AddViewEntity("header",llMain);
    }
}